<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SQLAbstract
 *
 * @author hodek
 */
abstract class SQLAbstract {

    
    public static $SELECT_TEXT = "SELECT * FROM text_ WHERE name = '%s'";
    public static $SELECT_ITEMSID = "SELECT DISTINCT a.itemid, b.data visibility FROM itemmap a LEFT JOIN number b ON (a.value_name = b.name) WHERE a.itemtype_name = '%s' AND a.item_name = 'VISIBILITY'";
    public static $SELECT_ITEM_ID_NAME = "SELECT e.data numberData, e.name numberName, b.data textData, b.name textName, c.data dateData, c.name dateName, d.idcismap dynData, d.name dynName FROM itemmap a LEFT JOIN text_ b ON (a.value_name = b.name) LEFT JOIN date c ON (a.value_name = c.name) LEFT JOIN dynstruc d ON (a.value_name = d.name) LEFT JOIN number e ON (a.value_name = e.name) WHERE a.itemid = %s AND a.item_name = '%s'";
    public static $SELECT_ITEMTYPE_NAME = "SELECT b.name, b.data FROM itemtype a JOIN text_ b ON (a.text_name = b.name)WHERE a.name = '%s'";    
    public static $SELECT_MANIFEST = "SELECT * FROM temp_manifest";
    public static $SELECT_USER = "SELECT * FROM `user` WHERE `login` = '%s' AND `pass` = '%s'";
    public static $SELECT_ALL_VALUES = 'SELECT data.data, data.tableName 
        FROM (SELECT name as name, data as data, 
        (SELECT DISTINCT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name = "number" ) 
        as tableName FROM number UNION SELECT name, data, 
        (SELECT DISTINCT  table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name = TRIM("text_ ") ) 
        FROM  text_  UNION SELECT name,%2$s, 
        (SELECT DISTINCT  table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name = "img" ) 
        FROM img UNION SELECT name,data,
        (SELECT DISTINCT  table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name = "date" )
        FROM date UNION SELECT name,idcismap as data,
        (SELECT DISTINCT  table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name = "dynstruc" )
        FROM dynstruc) AS data WHERE data.name = "%1$s"';
    
    public static $SELECT_DYNCISMAP = 'SELECT fkname FROM dyncismap WHERE id = %s';
    public static $SELECT_IMG_FULL = "SELECT fullSize,ext FROM img WHERE name LIKE '%s'";
    public static $SELECT_IMG_MEDIUM = "SELECT mediumSize,ext FROM img WHERE name LIKE '%s'";
    public static $SELECT_IMG_SMALL = "SELECT smallSize,ext FROM img WHERE name LIKE '%s'";
    public static $SELECT_ITEMMAP_CATPARTS = "SELECT item_name, value_name FROM itemmap WHERE itemType_name = '%s' AND itemid = %s";
    public static $SELECT_ITEMMAP_ITEMIDS = "SELECT itemid FROM itemmap WHERE itemType_name = '%s'";
    public static $SELECT_ITEMMAP_ITEMID = "SELECT itemid FROM itemmap WHERE value_name = '%s'";
    
    
    public static $INSERT_SQLDEBUG = "INSERT INTO `sqldebug` (`id` ,`query` ,`time` ,`post`, `session`)VALUES (NULL , '%s',CURRENT_TIMESTAMP , '%s', '%s');";
    public static $INSERT_MANIFEST_ITEM = "INSERT INTO temp_manifest VALUES (null,'%s','%s','%s')";
    public static $INSERT_ITEMTYPE = "INSERT INTO itemtype VALUES ('%s','%s')";
    public static $INSERT_ITEMMAP = "INSERT INTO itemmap VALUES ('%s', '%s', '%s', %s);";
    public static $INSERT_GENID = "INSERT INTO genid (id) VALUES (NULL);";
    public static $INSERT_TEXT = 'INSERT INTO text_ VALUES ("%1$s", "%2$s", %3$s,CURRENT_TIMESTAMP,0);';
    public static $INSERT_DATE = 'INSERT INTO date VALUES ("%s", %s, %s,CURRENT_TIMESTAMP,0)';
    public static $INSERT_DYN = 'INSERT INTO dynstruc VALUES ("%s", %s,CURRENT_TIMESTAMP,0, %s)';
    public static $INSERT_DYNCISMAP_SIMPLE = 'INSERT INTO dyncismap VALUES (NULL, %s,0,0, "%s")';
    public static $INSERT_IMG = "INSERT INTO `img` (`name` ,`editedBy` ,`date`,`preview`, `fullSize`, `mediumSize`, `smallSize`, ext) VALUES ('%s', '%s', CURRENT_TIMESTAMP,0, '%s', '%s', '%s', '%s'); ";
    public static $INSERT_NUMBER = 'INSERT INTO number VALUES ("%s", %s, %s, CURRENT_TIMESTAMP, 0);';
    public static $INSERT_USER = "INSERT INTO user VALUES (null, '%s', '2012-02-07 21:49:24', '%s', 0, '%s');";
    
    
    
    public static $UPDATE_DATA = "UPDATE %s SET data = '%s',editedBy = %s WHERE name = '%s'";
    public static $UPDATE_IMG = "UPDATE img SET editedBy = '%s', fullSize = '%s', mediumSize = '%s', smallSize = '%s', ext = '%s' WHERE name = '%s'";
    
    public static $DELETE_ITEM_BYITEMID = "DELETE a, img, number, text_cz, text_en, date, dyncismap, dynstruc FROM itemmap a LEFT JOIN img ON (a.value_name = img.name) LEFT JOIN text_cz ON (a.value_name = text_cz.name) LEFT JOIN text_en ON (a.value_name = text_en.name) LEFT JOIN number ON (a.value_name = number.name) LEFT JOIN date ON (a.value_name = date.name) LEFT JOIN dynstruc ON (a.value_name = dynstruc.name) LEFT JOIN dyncismap ON (dyncismap.id = dynstruc.idcismap) LEFT JOIN img img2 ON (img2.name = dyncismap.fkname) WHERE a.itemid = %s";
    public static $DELETE_SOMETING = 'DELETE %1$s FROM %1$s WHERE name = %2$s';
    public static $DELETE_USER = "DELETE user FROM user WHERE login = '%s'";
    public static $DELETE_dyncis = 'DELETE dyncismap FROM dyncismap WHERE fkname = "%s"';    
    
    public static $DROP_MANIFEST = "DROP TABLE temp_manifest";
    public static $CREATE_MANIFEST = "CREATE TABLE IF NOT EXISTS `temp_manifest` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `templateName` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,  
  `templatePath` varchar(250) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,  
  `role` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";    
    
    public static $table_text_name = "";

    public static function init() {
        $text = "text_" . getLangName() . " ";
        /*SQLAbstract::$table_text_name = trim($text);
        $langs = $GLOBALS['langList'];
        $temp_insert_text = "";
        foreach ($langs as $value) {
            $temp_insert_text .= str_replace("text_ ", "text_".$value::getClass()." ", SQL::$INSERT_TEXT);                 
        } */       
        //SQL::$INSERT_TEXT = $temp_insert_text;
                
        SQL::$SELECT_TEXT = str_replace("text_ ", $text, SQL::$SELECT_TEXT);        
        SQL::$SELECT_ITEM_ID_NAME = str_replace("text_ ", $text, SQL::$SELECT_ITEM_ID_NAME);
        SQL::$SELECT_ITEMTYPE_NAME = str_replace("text_ ", $text, SQL::$SELECT_ITEMTYPE_NAME);
        SQL::$SELECT_ITEMSID = str_replace("text_ ", $text, SQL::$SELECT_ITEMSID);        
        SQL::$SELECT_ALL_VALUES = str_replace("text_ ", $text, SQL::$SELECT_ALL_VALUES);
    }

}


?>
